<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>动漫管理</title>
    <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.0.min.js"></script>
    <script>
        let NowTotal;
        let NowNum;
        let NowCurrentPage;
        let search;
        $(document).ready(function () {
            initAnim(NowTotal, NowNum, NowCurrentPage,search);

            $("#search").click(function () {
                NowTotal = undefined;
                search = $("#searchName").val();
                initAnim(NowTotal, NowNum, NowCurrentPage,search);
            })

        })

        function initAnim(total,num,currentPage,search) {
            $.ajax({
                url:"${pageContext.request.contextPath}/admin/getAnim",
                data:{"total":total,"num":num,"currentPage":currentPage,"search":search},
                type:"post",
                dataType:"json",
                success:function (data) {
                    if (data.flag){
                        let limit = data.data.limit;
                        data = data.data.list;
                        NowTotal= limit.total;
                        NowNum = limit.num;
                        NowCurrentPage = limit.currentPage;
                        let limitContext = ``;
                        let tableContext = ``;
                        for (let i = 0; i < data.length; i++) {
                            tableContext = tableContext + `
                                <tr>
                                    <td>` + data[i].animationSimple.aid + `</td>
                                    <td>` + data[i].animationSimple.aname + `</td>
                                    <td>` + data[i].animationSimple.aname_jp + `</td>
                                    <td style="width: 100px"><img style="width: 100%" src="${pageContext.request.contextPath}` + data[i].animationSimple.cover_img + `"></td>
                                    <td>` + data[i].animationSimple.flash_time + `</td>
                                    <td>` + data[i].animationSimple.state + `</td>
                                    <td>` + data[i].animationSimple.score + `</td>
                                    <td>` + getDateTime(data[i].animationSimple.create_time) + `</td>
                                    <td>` + data[i].animationSimple.author + `</td>
                                    <td>` + data[i].category.cname + `</td>
                                    <td>` + data[i].animationSimple.simple_introduce + `</td>
                                    <td>` + data[i].animationSimple.people_num + `</td>
                                    <td>` + data[i].animationSimple.production_company + `</td>
                                    <td>` + data[i].animationDatailed.pv + `</td>
                                    <td style="width: 400px">` + data[i].animationDatailed.detailed + `</td>
                                    <td>` + data[i].animationDatailed.num + `</td>
                                    <td>
                                        <a class="btn btn-default btn-sm delete-anim">删除</a>&nbsp;
                                        <a class="btn btn-default btn-sm update-anim" href="${pageContext.request.contextPath}/admin/toUpdateAnimation?aid=` + data[i].animationSimple.aid + `">修改</a>
                                    </td>
                                </tr>
                            `;
                        }

                        if (limit.currentPage == 1) {
                            limitContext = limitContext + `
                                <li class="disabled"><a>上一页</a></li>
                            `
                        } else {
                            limitContext = limitContext + `
                                <li><a>上一页</a></li>
                            `
                        }
                        for (let i = 0; i < limit.pages.length; i++) {
                            if (limit.pages[i] == limit.currentPage) {
                                limitContext = limitContext + `
                                    <li class="active"><a>` + limit.pages[i] + `</a></li>
                                `;
                            } else {
                                limitContext = limitContext + `
                                    <li><a>` + limit.pages[i] + `</a></li>
                                `;
                            }
                        }
                        if (limit.currentPage == limit.maxPage) {
                            limitContext = limitContext + `
                                <li class="disabled"><a>下一页</a></li>
                            `
                        } else {
                            limitContext = limitContext + `
                                <li><a>下一页</a></li>
                            `
                        }
                        limitContext = limitContext + `
                            <p>共<span id="total">` + limit.total + `</span>条记录</p>
                        `
                        $("#limit").html(limitContext);
                        $("#body").html(tableContext);
                        addLimitClick();
                        addDeleteClick();
                    } else {
                        $("#limit").html(``);
                        $("#body").html(data.errorMsg);
                    }
                }
            })
        }
        
        function addDeleteClick() {
            $(".delete-anim").click(function () {
                let r = confirm("确认删除吗");
                if (r == true) {
                    // 删除
                    $.post("${pageContext.request.contextPath}/adminAnim/deleteAnim",{"aid":
                            $(this).parent().parent().children().first().html()},function (data) {
                        if (!data.flag) {
                            alert("删除失败！")
                        } else {
                            initAnim(NowTotal, NowNum, NowCurrentPage,search);
                        }
                    },"json")

                }
            })
        }
        
        function addLimitClick() {
            $("#limit>li").click(function () {
                if (!($(this).hasClass("disabled") || $(this).hasClass("active"))) {
                    let total;
                    let num;
                    let currentPage;
                    let p = $(this).children().html();
                    if (p == "上一页") {
                        currentPage = NowCurrentPage - 1;
                    } else if (p == "下一页") {
                        currentPage = NowCurrentPage + 1;
                    } else {
                        currentPage = p;
                    }
                    total = NowTotal;
                    num = NowNum;
                    initAnim(total, num, currentPage);
                }
            })
        }
        
        function toAddAnimation(){
                location.href= "${pageContext.request.contextPath}/admin/toAddAnimation";
        }

        function getDateTime(date) {
            date = new Date(parseInt(date, 10));
            var year = date.getFullYear();

            var month = date.getMonth() + 1;

            var day = date.getDate();

            var hh = date.getHours();

            var mm = date.getMinutes();

            var ss = date.getSeconds();

            return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
        }
    </script>
    <style>
        .table td{
            font-size: 10px;
        }


        #limit>p{
            font-size: 20px;
            display: inline;
            margin-left: 10px;
            color: #18abef
        }

        #limit>p>span{
            color: #18abef
        }
    </style>
</head>

<body>
<div class="container"  style="width: 100%">
    <div class="row"><c:import url="component/header.jsp"></c:import></div><br><br><br>
    <h1>动漫管理系统</h1>
    <div class="row">
            <div class="form-group col-lg-3">
                <input id="searchName" type="text" class="form-control" placeholder="请输入用户名">
            </div>
            <button id="search" class="btn btn-default">搜索</button>
            <a type="button" id="resolveSelected" class="btn btn-default" href="${pageContext.request.contextPath}/admin/toAddAnimation">增加动漫</a>
    </div>
    <form id="form" action="" method="post">
        <table border="1" class="table table-bordered table-hover">
            <tr id="head" class="default">
                <th>动漫ID</th>
                <th>动漫名</th>
                <th>动漫原名</th>
                <th>封面</th>
                <th>更新时间</th>
                <th>状态</th>
                <th>分数</th>
                <th>定档时间</th>
                <th>作者</th>
                <th>类型</th>
                <th>简单简介</th>
                <th>评论人数</th>
                <th>制作公司</th>
                <th>pv</th>
                <th>详细简介</th>
                <th>集数</th>
                <th>操作</th>
            </tr>
            <tbody id="body">
            </tbody>
        </table>
    </form>
    <ul class="pagination" id="limit">
    </ul>
</div>

</body>
</html>
